﻿using System;
using System.Collections.Generic;

using System.Text;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;

namespace lxwmvc
{
    /// <summary>
    /// 功能:字符串操作类
    /// [09.4.10 lxw]<para />
    /// </summary>
    public class stringhelper
    {
        /// <summary>
        /// 功能:不区分大小写替换文本
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strsource">查找字符串</param>
        /// <param name="strfind">需要被替换的字符串</param>
        /// <param name="strreplace">替换后字符串</param>
        /// <returns>返回全部替换后的字符串</returns>
        public static string replace(string strsource, string strfind, string strreplace)
        {
            return Strings.Replace(strsource, strfind, strreplace, 1, -1, CompareMethod.Text);
        }

        /// <summary>
        /// 功能:把数字转换成对应的字符
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="charcode">需要转换的数字</param>
        /// <returns>转换后的字符</returns>
        public static char chr(int charcode)
        {
            return Strings.Chr(charcode);
        }

        /// <summary>
        /// 功能:把字符转换成对应的asc码
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strchar">需要转换的字符</param>
        /// <returns>转换后的asc吗</returns>
        public static int asc(string strchar)
        {
            return Strings.Asc(strchar);
        }

        /// <summary>
        /// 功能:查找第二个字符串在第一个字符串所在的其实位置
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strsource">原字符串</param>
        /// <param name="strsrc">需要查找的字符串</param>
        /// <returns>返回整数，第二个字符串在第一个字符串的起始位置</returns>
        public static int instr(string strsource, string strsrc)
        {
            return Strings.InStr(strsource, strsrc, CompareMethod.Text) - 1;
        }

        /// <summary>
        /// 功能:分割字符串为字符串数组
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strsource">需要分割的字符串</param>
        /// <param name="strsplit">用于分割的字符串</param>
        /// <returns>已经分割的字符串数组</returns>
        public static string[] split(string strsource, string strsplit)
        {
            return Strings.Split(strsource, strsplit, -1, CompareMethod.Text);
        }

        /// <summary>
        /// 功能:从字符串中提出制定的字符串，strSource中每个字符串用","隔开
        /// [11-03-08 16:01  周二 lxw]<para />
        /// </summary>
        /// <param name="strsource">源字符串</param>
        /// <param name="strfindvalue">需要查找的字符串</param>
        /// <returns>提出后的字符串</returns>
        public static string splice(string strsource, string strfindvalue)
        {
            if (strsource == "") return "";
            strsource = "," + strsource + ",";
            strsource.Replace("," + strfindvalue + ",", ",");

            strsource = strsource.StartsWith(",") ? strsource.Substring(1) : strsource;
            strsource = strsource.EndsWith(",") ? strsource.Substring(0, strsource.Length -1) : strsource;
            return strsource;
        }

        /// <summary>
        /// 功能:从字符串中查找是否存在指定的字符，strSource中每个字符串用","隔开
        /// [11-03-08 16:01  周二 lxw]<para />
        /// </summary>
        /// <param name="strsource">源字符串</param>
        /// <param name="strfindvalue">需要查找的字符串</param>
        /// <param name="bignorecase">是否忽略大小写，默认为true，忽略</param>
        /// <returns>true:查找到</returns>
        public static bool findvalue(string strsource, string strfindvalue, bool bignorecase = true)
        {
            if (string.IsNullOrEmpty(strfindvalue)) return false;

            foreach (string s in strsource.Split(','))
            {
                if (s.Equals(strfindvalue, bignorecase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)) return true;
            }
            return false;
        }

        /// <summary>
        /// 功能:返回一个字符串，该字符串包含某字符串中指定数量的字符,跟substring 类似
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strsource">原字符串</param>
        /// <param name="intstart">起始位置</param>
        /// <param name="intlength">字符串长度，如果为0，代表从起始位置以后的所有字符</param>
        /// <returns>返回指定数量的字符串</returns>
        public static string mid(string strsource, int intstart, int intlength)
        {
            if (intlength == 0)
            {
                return Strings.Mid(strsource, intstart);
            }

            return Strings.Mid(strsource, intstart, intlength);
        }

        /// <summary>
        /// 功能:html字符串编码为普通字符串，不被浏览器解析<para />
        /// 比如:Replace(">", "&gt;");<para />
        /// [09.4.11 lxw]<para />
        /// </summary>
        /// <param name="strsource">需要编码的html字符串</param>
        /// <returns>返回已经编码过的字符串</returns>
        public static string htmlencode(string strsource)
        {
            string strEncode = "";
            if (!string.IsNullOrEmpty(strsource))
            {
                strEncode = replace(strsource, ">", "&gt;");
                strEncode = replace(strEncode, "<", "&lt;");
                strEncode = replace(strEncode, chr(0x20).ToString(), " ");
                strEncode = replace(strEncode, chr(9).ToString(), " ");
                strEncode = replace(strEncode, chr(0x22).ToString(), "&quot;");
                strEncode = replace(strEncode, chr(0x27).ToString(), "&#39;");
                strEncode = replace(strEncode, chr(13).ToString(), "");

                //这句是不是有点问题？
                //[11-03-11 09:31 lxw]
                //strEncode = replace(strEncode, chr(10).ToString(), "<BR> ");

                //strEncode = Replace(strEncode, Chr(10).ToString(), "<BR> ");
                //strEncode = blnFilterBadWords ? ChkBadWords(strEncode) : strEncode;
            }
            return strEncode;
        }

        /// <summary>
        /// 功能:对字符串进行HTML解码操作，解码为客户端浏览器可解析的html字符<para />
        /// 比如:Replace("&gt;", ">");<para />
        /// [09.4.12 lxw]<para />
        /// </summary>
        /// <param name="strsource">需要解码的字符串</param>
        /// <returns>返回解码后的字符串</returns>
        public static string htmldecode(string strsource)
        {
            if (string.IsNullOrEmpty(strsource)) return string.Empty;

            string strResult = strsource.ToString();
            strResult = strResult.Replace("&amp;", "&");
            strResult = strResult.Replace("&gt;", ">");
            strResult = strResult.Replace("&lt;", "<");
            strResult = strResult.Replace(" ", chr(0x20).ToString());
            strResult = strResult.Replace(" ", chr(9).ToString());
            strResult = strResult.Replace("&quot;", chr(0x22).ToString());
            strResult = strResult.Replace("&#39;", chr(0x27).ToString());
            strResult = strResult.Replace("", chr(13).ToString());
            //fString = fString.Replace("</P><P> ",TopHelper.Chr(10).ToString() + TopHelper.Chr(10).ToString());
            strResult = strResult.Replace("<BR> ", chr(10).ToString());

            

            return strResult;
        }

        /// <summary>
        /// 功能:过滤所有的HTML字符串，只保留其中内容
        /// [09.4.12 lxw]<para />
        /// </summary>
        /// <param name="strsource">需要被过滤的字符串</param>
        /// <returns>返回过滤后的字符串</returns>
        public static string replacehtmlstring(string strsource)
        {
            return Regex.Replace(strsource, @"<[^>]+>", "");
        }

        /// <summary>
        /// 功能:截取字符串,不会因为中英文宽度不一样而出现整个字符串长度不一样（是不是都有效果？）
        /// [09.4.12 lxw]<para />
        /// </summary>
        /// <param name="strsource">需要截取的字符串</param>
        /// <param name="intlength">截取后字符串的长度</param>
        /// <param name="strendstring">结尾字符串，如果字符串被截取，后面用加结尾字符串,没有截取就不用加结尾字符串</param>
        /// <param name="strothers">空格在这里也是宽字符</param>
        /// <returns>返回截取后的字符串</returns>
        public static string cutstr(string strsource, int intlength, string strendstring = "",string strothers = " ")
        {
            int count = 0;
            string strResult = string.Empty;
            if (string.IsNullOrEmpty(strsource)) return strResult;

            for (int i = 0; i < strsource.Length; i++)
            {
                //空格在计算的时候也应该是两个字符
                count += (Regex.IsMatch(strsource[i].ToString(), "[^\x00-\xff]") || strothers.IndexOf(strsource[i]) !=-1 ) ? 2 : 1;
                if (count >= intlength)
                {
                    try
                    {
                        strResult = strsource.Substring(0, i + 1) + strendstring;
                        break;
                    }
                    catch
                    {
                        strResult = strsource.Substring(0, i) + strendstring;
                        break;
                    }
                }
                else
                    strResult = strsource;
            }

            strResult = strResult.Replace("\n", "");
            return strResult;
        }

        /// <summary>
        /// 功能:兼容php方法,去掉结尾字符串
        /// [11-03-08 16:01  周二 lxw]<para />
        /// </summary>
        /// <param name="strsource">源字符串</param>
        /// <param name="strend">结尾字符串</param>
        /// <returns>返回去掉结尾后的字符串</returns>
        public static string strend(string strsource, string strend)
        {
            return strsource.EndsWith(strend) ? strsource.Substring(0, strsource.Length - strend.Length) : strsource;
        }

        /// <summary>
        /// 功能:去掉头部的字符串
        /// [10.3.1 hanlei]<para />
        /// </summary>
        /// <param name="strsource">源字符串</param>
        /// <param name="strstart">待去掉的字符串</param>
        /// <returns></returns>
        public static string strstart(string strsource, string strstart)
        {
            return strsource.StartsWith(strstart) ? strsource.Substring(1, strsource.Length - strstart.Length) : strsource;
        }

        /// <summary>
        /// 功能:将\r\n转化为 br
        /// [11-03-08 16:00  周二 lxw]<para />
        /// </summary>
        /// <param name="strsource">原字符串</param>
        /// <returns>返回新的字符串</returns>
        public static string nr2br(string strsource)
        {
            return strsource.Replace("\r\n", "<br/>").Replace("\n", "<br/>");
        }

        //public static string utc(object utctime)
        //{
        //    if (utctime.tostr().Length>5)
        //    {
        //        return utctime.toUTCTime().ToString();
        //    }
        //    else
        //    {
        //        return utctime.ToString();
        //    }
           
        //}
    }
}
